$(function() {

	// 学生数据
	// layui表格
	layui.use([ 'table', 'form','layer','upload','element' ],function() {
						var table = layui.table;
						var form = layui.form;
						var layer = layui.layer;
						var upload = layui.upload;
						var element= layui.element;

						var T = table.render({
							elem : '#StudentTable',
							page:true,
							limit:10,
							limits:[5,10,15,20],
							url : '/selectStudent', // 数据接口
							response : {
								statusCode : 200
							// 重新规定成功的状态码为 200，table 组件默认为 0
							},
							parseData : function(res) { // res 即为原始返回的数据
								return {
									"count" : res.tol,
									"code" : res.status, // 解析接口状态
									"msg" : res.message, // 解析提示文本
									"data" : res.list
								// 解析数据列表
								};
							},
							cols : [ [ // 表头
							{
								field : 'stuId',
								title : '工号',
								width : 200,
								sort : true,
								align:'center',
								fixed : 'left'
							}, {
								field : 'stuName',
								title : '学生名字',
								align:'center',
							}, {
								field : 'className',
								align:'center',
								title : '班级'
							}, {
								field : 'deptName',
								align:'center',
								title : '院系'
							}, {
								field : 'operate',
								toolbar : '#barDemo',
								align:'center',
								title : '操作',
								width : 230
							} ] ]
						});

						// 监听工具
						table.on('tool(StudentTable)',function(obj) {
											var data = obj.data;
											console.log(data);
											// 监听删除按钮
											if (obj.event == 'del') {
												layer.confirm('确定删除吗？',function(index) {
													$.ajax({
														url : "/del",
														type : "post",
														data : {
															stuId : data.stuId
														},
														dataType : "json",
														success : function() {
															obj.del();
															layer.close(index);
															layer.msg("删除成功");
														},
														error : function() {
															layer.msg("error");
															layer.close(index);
															}
														})
												});
											} else if (obj.event == 'edit') {
												layer.open({
															title:'学生信息',
															type : 1,
															skin : 'layui-layer-rim', // 加上边框
															area : [ '520px',
																	'400px' ], // 宽高
															content : '<form class="layui-form layui-form-pane" action="#" id="form1">\n'
																	+ '  <br>\n'
																	+ '  <div class="layui-form-item">\n'
																	+ '    <label class="layui-form-label">学号</label>\n'
																	+ '    <div class="layui-input-block" >\n'
																	+ '      <input type="text" id="input1" name="stuId" lay-verify="title" autocomplete="off" readonly disabled class="layui-input form-control" >\n'
																	+ '    </div>\n'
																	+ '  </div>\n'
																	+ '  <div class="layui-form-item">\n'
																	+ '    <label class="layui-form-label">姓名</label>\n'
																	+ '    <div class="layui-input-block">\n'
																	+ '      <input type="text" id="input2" name="stuName" lay-verify="title" autocomplete="off" value=stuName  disabled readonly class="layui-input form-control">\n'
																	+ '    </div>\n'
																	+ '  </div>\n'
																	+ '  <div class="layui-form-item">\n'
																	+ '    <label class="layui-form-label">院系</label>\n'
																	+ '    <div class="layui-input-block">\n'
																	+ '      <select id="select1" name="deptName" lay-filter="select1" lay-verify="">\n'
																	+ '        <option></option>\n'
																	+ '      </select>\n'
																	+ '    </div>\n'
																	+ '  </div>'
																	+ '  <div class="layui-form-item">\n'
																	+ '    <label class="layui-form-label">班级</label>\n'
																	+ '    <div class="layui-input-block">\n'
																	+ '      <select id="select2" name="className" disabled lay-verify="">\n'
																	+ '        <option></option>\n'
																	+ '      </select>\n'
																	+ '    </div>\n'
																	+ '  </div>\n'
																	+ '<div><button  id="form1_btn" lay-submit lay-filter="btn1" class="layui-btn" style="margin-left: 160px;">提交</button></div>\n'
																	
																	+ '</form>'
														});
												var stuId = data.stuId;
												var stuName = data.stuName;
												var className = data.className;
												var deptName = data.deptName;
														
												console.log(stuId);
												console.log(stuName);
												console.log(className);
												console.log(deptName);
														
												// 添加数据到表
												$("#input1").val(stuId);// 学生ID
												$("#input2").val(stuName);// 学生姓名
//												$("#from1 [name='deptName']>option:eq(0)").text(deptName);// 院系
//												$("#from1 [name='className']:eq(0)").text(className);// 班级

												$.ajax({
													url : '/selectDept',
													dataType : 'json',
													type : 'post',
													success : function(data) {
														var d = eval(data);
														console.log(d);
														for (var i = 0; i < d.length; i++) {
															$("#select1").append("<option value="+d[i].deptId+">"+d[i].deptName+"</option>")
															console.log("......"+d[i].deptName+".."+d[i].deptId);
															form.render('select');
														}
													}
												})
														
												form.on('select(select1)', function(data){
													var select1 = data.value;//得到被选中的值
													console.log(select1);
															 
													if(select1 != null && select1 !=''){
														$("#select2").attr("disabled",false);
														$.ajax({
															url :'/selectC',
															dataType : 'json',
															type : 'post',
															data:{
																"deptId":select1
															},
															success : function(data) {
																var d = eval(data);
																console.log(d);
																$("#select2").empty();
																$("#select2").append("<option></option>");
																for (var i = 0; i < d.length; i++) {
																	$("#select2").append("<option value="+d[i].classId+">"+d[i].className+"</option>")
																	console.log("......"+d[i].className+".."+d[i].classId);
																	form.render('select');
																}
															},error:function(){
																layer.alert('系统错误',{
																	icon : 2,
																	skin : 'layer-ext-moon'
																})
															}
														})
													}else{
														$("#select2").empty();
														$("#select2").append("<option></option>");
														$("#select2").attr("disabled",true);
														form.render('select');
																  
													}
												}); 
												//修改提交
												form.on('submit(btn1)', function(data){
													
													console.log(data);
													var formData = data.field;
													var deptId = formData.deptName;
													var classId = formData.className;
													var stuId = formData.stuId;
													
													
	//												var deptName = $("#select1 option:selected").text();
	//												var deptId = $("#select1 option:selected").val();
	//												var className = $("#select2 option:selected").text();
	//												var classId = $("#select2 option:selected").val();
													console.log(deptId);
													console.log(classId);
													console.log(stuId);
																
													if(deptName !=null && className!= null && deptName != '' && className != ''){
														$.ajax({
														url:'/updateStudent',
														dataType:'json',
														type:'post',
														data:{
															"stuId":stuId,
															"deptId":deptId,
															"classId":classId
														},
														success:function(r){
															if(r.msg =="YES"){
																layer.closeAll();
																T.reload();
															}
															layer.msg("信息修改成功");
														},
														error:function(){
															layer.alert('系统错误',{
																icon : 2,
																skin : 'layer-ext-moon'
															})
														}
													})
												}else{
													layer.alert('请将信息填写完整', {
														icon: 0,
														skin: 'layer-ext-moon' 
														})
												}
													return false;
										})			
									}else if(obj.event = "RP"){
										layer.confirm('确定重置密码？',function(index) {
										
										
											$.ajax({
												url:'/RP',
												dataType:'json',
												type:'post',
												data:{
													stuId : data.stuId
												},success:function(res){
													if(res.msg == 'YES'){
														layer.msg("密码重置成功");
													}else{
														layer.msg("密码重置失败");
													}
												},error:function(){
													layer.alert('系统错误',{
														icon : 2,
														skin : 'layer-ext-moon'
													})
												}
											})
										})
									}
									form.render();
								})

						// 院系按钮显示
						layui.use('layer', function() {
							var layer = layui.layer;
							$.ajax({
								url : '/selectDept',
								dataType : 'json',
								type : 'post',
								success : function(data) {
									var da = eval(data);
									console.log(da);

									for (var i = 0; i < da.length; i++) {
										$("#department_select2").append(
												"<button type=\"button\" class=\"btn btn-default\">"
														+ da[i].deptName
														+ "</button>");
									}
								},
								error : function() {
									layer.alert('院系信息读取失败', {
										icon : 2,
										skin : 'layer-ext-moon'
									})
								}
							})

						})

						
						
						// 院系选择显示并查询其学生数据及班级

						$("#department_select2")
								.on(
										"click",
										"button",
										function() {
											var deptName = $(this).text();
											console.log(deptName);
											$("#span1").html(deptName);
											$("#span2").html("全部班级");
											$("#select_input2").val("");
											if (deptName == "全部院系") {
												$("#class_select>button:gt(0)").remove();
												layui.use('table',function() {
													var table = layui.table;
													table.reload('StudentTable',{
														url : '/selectStudent',
														page : {
															curr : 1// 重新从第1页开始
														}
													})
												})
											} else {
												$.ajax({
													url : '/selectClass',
													data : {
														"deptName" : deptName
													},
													dataType : 'json',
													type : 'post',
													success : function(data) {
														$("#class_select>button:gt(0)").remove();
														var da = eval(data);
														console.log(da);
														for (var i = 0; i < da.length; i++) {
															$("#class_select").append("<button type=\"button\" class=\"btn btn-default\">"+ da[i].className+ "</button>");
														}
													},
													error : function() {
														layer.alert('班级信息读取失败',{
															icon : 2,
															skin : 'layer-ext-moon'
														})
													}
												})

												layui.use('table',function() {
																	var table = layui.table;
																	table.reload('StudentTable',{
																		url : '/selectDept_student',
																		where : {
																			deptName : deptName
																		},
																		page : {
																			curr : 1
																			}
																	})

												})
											}

										})
						// 班级选择并查询学生
						$("#class_select").on("click", "button", function() {

							var className = $(this).text();
							$("#span2").html(className);
							var deptName = $("#span1").text();
							$("#select_input2").val("");
							
							console.log("点击班级按钮显示当前院系。。。。。。。。"+deptName);
							console.log("+++++++++++++");
							console.log(className);
							
							if(className == "全部班级" ){
								layui.use('table', function() {
									var table = layui.table;
									table.reload('StudentTable', {
										url : '/selectDept_student',
										where : {
											deptName:deptName
										},
										page : {
											curr : 1
										// 重新从第 1 页开始
										}
									})

								})
							}else{
								layui.use('table', function() {
									var table = layui.table;
									table.reload('StudentTable', {
										url : '/selectCalss_student',
										where : {
											className : className
										},
										page : {
											curr : 1
										// 重新从第 1 页开始
										}
									})

								})
							}
							
							

						})

						

						// 添加学生
						$("#Add_Student").on("click",
							function() {
								layer.open({
									title:'请填写添加学生的信息',
									type : 1,
									skin : 'layui-layer-rim', // 加上边框
									area : [ '550px','450px' ], // 宽高
									content : '<form class="layui-form layui-form-pane" id="form2" action="#">\n'
											+ '  <br>\n'
											+ '  <div class="layui-form-item">\n'
											+ '    <label class="layui-form-label">学号</label>\n'
											+ '    <div class="layui-input-block">\n'
											+ '      <input type="text" id="inp_stuId" name="stuId" lay-verify="stuId" autocomplete="off" placeholder="请填写学生学号"   class="layui-input" >\n'
											+ '    </div>\n'
											+ '  </div>\n'
											+ '  <div class="layui-form-item">\n'
											+ '    <label class="layui-form-label">姓名</label>\n'
											+ '    <div class="layui-input-block">\n'
											+ '      <input type="text" id="inp_stuName" name="stuName" lay-verify="stuId" autocomplete="off" placeholder="不能为空"  class="layui-input">\n'
											+ '    </div>\n'
											+ '  </div>\n'
											+ '  <div class="layui-form-item">\n'
											+ '    <label class="layui-form-label">密码</label>\n'
											+ '    <div class="layui-input-block">\n'
											+ '      <input type="text" id="inp_password" name="password" lay-verify="title" autocomplete="off" placeholder="123456(初始密码)"   readonly disabled class="layui-input form-control">\n'
											+ '    </div>\n'
											+ '  </div>\n'
											+ '  <div class="layui-form-item">\n'
											+ '    <label class="layui-form-label">院系</label>\n'
											+ '    <div class="layui-input-block">\n'
											+ '      <select id="select3" name="deptName" lay-filter="select3" lay-verify="">\n'
											+ '        <option ></option>\n'
											+ '      </select>\n'
											+ '    </div>\n'
											+ '  </div>\n'
											+ '  <div class="layui-form-item">\n'
											+ '    <label class="layui-form-label">班级</label>\n'
											+ '    <div class="layui-input-block">\n'
											+ '      <select id="select4" name="className" disabled lay-filter="select4" lay-verify="">\n'
											+ '        <option ></option>\n'
											+ '      </select>\n'
											+ '    </div>\n'
											+ '  </div>\n'
											+ '<div ><button id="form2_btn" lay-submit lay-filter="btn2" class="layui-btn" style="margin-left: 160px;">提交</button>\n'
											+ '<button type="reset" class="layui-btn layui-btn-primary">重置</button></div>\n'
											+ '</form>'
									});
									form.render();
											
									$.ajax({
										url : '/selectDept',
										dataType : 'json',
										type : 'post',
										success : function(data) {
											var d = eval(data);
											console.log(d);
											for (var i = 0; i < d.length; i++) {
												$("#select3").append("<option value="+d[i].deptId+">"+d[i].deptName+"</option>")
												console.log("......"+d[i].deptName+".."+d[i].deptId);
												form.render('select');
											}
										}
									})
									form.on('select(select3)', function(data){
										var select3 = data.value;//得到被选中的值
										
										console.log(select3);
												 
										if(select3 != null && select3 !=''){
											$("#select4").attr("disabled",false);
											$.ajax({
												url :'/selectC',
												dataType : 'json',
												type : 'post',
												data:{
													"deptId":select3
												},
												success : function(data) {
													var d = eval(data);
													console.log(d);
													$("#select4").empty();
													$("#select4").append("<option></option>");
													for (var i = 0; i < d.length; i++) {
														$("#select4").append("<option value="+d[i].classId+">"+d[i].className+"</option>")
														console.log("......"+d[i].className+".."+d[i].classId);
														form.render('select');
													}
												},error:function(){
													layer.alert('系统错误',{
														icon : 2,
														skin : 'layer-ext-moon'
													})
												}
											})
										}else{
											$("#select4").empty();
											$("#select4").append("<option></option>");
											$("#select4").attr("disabled",true);
											form.render('select');
										}
									});
									//添加提交
									form.on('submit(btn2)', function(data){
										var formData = data.field;
										var stuId = formData.stuId;
										var stuName = formData.stuName;
										var deptId = formData.deptName;
										var classId = formData.className;
										
										var str = /^[0-9]*[1-9][0-9]*$/
										console.log(stuId);
										console.log(stuName);
										console.log(deptId);
										console.log(classId);
										
										
										if((stuId !=null && stuId !='') && (stuName !=null && stuName !='') && (deptId !=null && deptId !='') && (classId !=null && classId !='')){
											if(str.test(stuId)){
												$.ajax({
													url:'/insertStudent',
													dataType:'json',
													type:'post',
													data:{
														"stuId":stuId,
														"stuName":stuName,
														"deptId":deptId,
														"classId":classId
													},
													success:function(r){
														if(r.msg=="YES"){
															layer.closeAll();
															T.reload();
															layer.msg("添加成功");
														}else{
															layer.alert('已有此学号，请输入新学号', {
																  icon: 0,
																  skin: 'layer-ext-moon' 
																})
														}
													},
													error:function(){
														layer.alert('系统错误', {
															  icon: 2,
															  skin: 'layer-ext-moon' 
															})
													}
												})
											}else{
												layer.msg("学号必须为纯数字");
											}
												
										}else{
											layer.msg("请将信息填写完整再提交");
										}
										
										return false;	
									})
								
											
							})

							// 模糊查询
							$("#btn_select2").on("click", function() {
								var msg = $("#select_input2").val();
								
								var deptName = $("#span1").text();
								var className = $("#span2").text();
								console.log("模糊查询显示当前院系。。。。。。。。"+deptName);
								console.log("模糊查询显示当前班级。。。。。。。。"+className);
								console.log(msg);
								if(msg != null && msg != ''){
									layui.use('table', function() {
										var table = layui.table;
										table.reload('StudentTable', {
											url : '/FuzzyQuery',
											where : {
												deptName:deptName,
												className:className,
												msg:msg
											},
											page : {
												curr : 1
											// 重新从第 1 页开始
											}
										})

									})
								}else{
									layer.msg("请在输入框输入需要搜索的信息");
								}
								
							})
							
							
			
	
	
			// 文件上传
			$("#Add_Students").on("click", function() {
				layer.open({
					  type: 1,
					  title:'请选择文件',
					  skin: 'layui-layer-rim', //加上边框
					  area: ['500px', '190px'], //宽高
					  content: '<div class="layui-upload">\n'
						  +'	<a style="color:blue" id="geshi"><span>查看上传文件格式</span></a>\n'
						 +'	<br>\n'
						 +'	<button type="button" class="layui-btn layui-btn-primary" id="choose">选择文件</button>\n' 
						 +'	<br>\n'
						 +'	<br>\n'
						 +'	<button type="button"  class="layui-btn layui-btn-fluid" id="upload">开始上传</button>\n'
						 +'	<br>\n'
						 +'	<br>\n'
						 +'	</div>'
					});
				
				
				$("body").on("click","#geshi",function(){
					
					layer.open({
						  type: 1,
						  title:'上传文件格式模板',
						  skin: 'layui-layer-rim', //加上边框
						  area: ['500px', '130px'], //宽高
						  content:'<table class="layui-table">\n'
							  +'<thead>\n'
							  +'<tr>\n'
							  +'<th>学号</th>\n'
							  +'<th>姓名</th>\n'
							  +'<th>学院名称</th>\n'
							  +'<th>班级名称</th>\n'
							  +'</tr>\n'
							  +'</thead>\n'
							  +'</table>'
						});
					
				})
				
				
				
					upload.render({
						elem:'#choose',
						url:'/upload',
						method:'post',
						auto:false,  //选择文件后不自动上传
						bindAction:'#upload',  //触发上传的按钮
						accept:'file',
						acceptMime:'file/xls,file/xlsx',
						exts:'xls|xlsx',
						number:1,
						choose:function(obj){
							console.log("2222222222");
							obj.preview(function(index, file, result){
							      console.log(index); //得到文件索引
							      console.log(file); //得到文件对象
							      $(".layui-upload>span").remove();
							      $("#choose").after("<span>&nbsp;&nbsp;已选中文件：<strong>"+file.name+"["+file.size/1024+"kb]</strong></span>");//将选中的文件名显示出来
							})
						},
						before:function(obj){
							layer.load();
						},
						done:function(res,index,upload){
							
							
							if(res.msg == "导入成功" || res.msg == "更新成功"){
								layer.closeAll();
								T.reload();
								layer.msg(res.msg);
							}else{
								layer.closeAll('loading');
								layer.msg(res.msg);
							}
							
							
						},
						error:function(index,upload){
							layer.closeAll('loading');
							layer.msg('系统错误');
						}
						
						
					})
				
				
				
			})

	});
})